/* ----------------------------------------------------------------------
 *	Shadow Quest
 *	Copyright (C) 2009 Prageeth Silva <prageeth@thenewcoders.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  ----------------------------------------------------------------------
 */

package panel;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

/** A Menu Button
 * 
 */
public class PanelButton
{

	private float x, y;
	
	private boolean hover, clicked, down;
	private boolean toggle, selected, shown;
	private Image hov, sel;
	

	public PanelButton(Image hov, Image sel, float x, float y)
	{
		
		this.setSelected(false);
		this.setClicked(false);
		this.setHover(false);
		this.setToggle(false);
		this.setShown(true);
		
		this.setHoverImage(hov);
		this.setSelectedImage(sel);
		
	}
	
	/** Render the button
     * @param g The Slick graphics object, used for drawing.
     */
    public void render(Graphics g)
    throws SlickException
    {
    	if (this.isShown()) 
    	{
    		if (this.isToggle() && this.isSelected())
    		{
    			this.getSelectedImage().draw(this.getX(),this.getY());
    		}
    		else if (this.isHover() && !this.isDown()) 
    		{
    			this.getHoverImage().draw(this.getX(),this.getY());
    		}
    	}
    }
    
	/** Update the button state for a frame.
	 * @param mousex The x coordinate of the mouse cursor
	 * @param mousey The x coordinate of the mouse cursor
	 * @param mousedown The status of the mouse button being down
	 * @param delta Time passed since last frame (milliseconds).
     */
    public void update(float mousex, float mousey, boolean mousedown, int delta)
    throws SlickException
    {
    	if (this.isShown()) 
    	{
    		this.setHover(this.isOnButton(mousex, mousey));
    		this.setDown(mousedown && this.isHover());
    	}
    }
    
    /** Checks if button is being pressed.
     * @param x The x coordinate of the mouse pointer.
     * @param y The y coordinate of the mouse pointer.
     */
	public boolean isOnButton(float x, float y)
	{
		double x1 = this.getX() + this.getWidth();
		double y1 = this.getY() + this.getHeight();
		return (x >= this.getX()) &&  (x <= x1) && (y >= this.getY()) && (y <= y1);
	}
	
	private float getHeight() 
	{
		return this.getHoverImage().getHeight();
	}
	
	/** Get the Width of the player.
     * @return The width.
     */
	private float getWidth() 
	{
		return this.getHoverImage().getWidth();
	}

	public float getX()
	{
		return this.x;
	}

	public void setX(float x)
	{
		this.x = x;
	}

	public float getY()
	{
		return this.y;
	}

	public void setY(float y)
	{
		this.y = y;
	}

	public boolean isHover() 
	{
		return this.hover;
	}

	public void setHover(boolean hover)
	{
		this.hover = hover;
	}

	public boolean isClicked() 
	{
		return this.clicked;
	}

	public void setClicked(boolean clicked) 
	{
		this.clicked = clicked;
	}

	public boolean isSelected()
	{
		return this.selected;
	}

	public void setSelected(boolean selected)
	{
		this.selected = selected;
	}

	public boolean isToggle()
	{
		return this.toggle;
	}

	public void setToggle(boolean toggle)
	{
		this.toggle = toggle;
	}

	public Image getHoverImage()
	{
		return this.hov;
	}

	public void setHoverImage(Image hov)
	{
		this.hov = hov;
	}

	public Image getSelectedImage() 
	{
		return this.sel;
	}

	public void setSelectedImage(Image sel)
	{
		this.sel = sel;
	}

	public boolean isShown() 
	{
		return this.shown;
	}

	public void setShown(boolean shown)
	{
		this.shown = shown;
	}

	public boolean isDown()
	{
		return down;
	}

	public void setDown(boolean down) 
	{
		this.down = down;
	}	
	
}
